package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAdmServicePage;
import com.zb.entity.vo.ServicePageListVo;
import com.zb.entity.vo.ServicePageSelectVo;
import com.zb.entity.vo.ServicePageVo;
import com.zb.mapper.ZjAdmServicePageMapper;
import com.zb.service.ZjAdmServicePageService;
import com.zb.utils.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 总后台服务页面表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-31
 */
@Service
public class ZjAdmServicePageServiceImpl extends ServiceImpl<ZjAdmServicePageMapper, ZjAdmServicePage> implements ZjAdmServicePageService {
    @Resource
    private ZjAdmServicePageMapper admServicePageMapper;

    // 新增或修改服务页面
    @Override
    public void save(ServicePageVo servicePageVo) {
        Assert.isTrue(null == admServicePageMapper.exstByName(servicePageVo.getId(), servicePageVo.getType(), servicePageVo.getName()), "名称已重复");
        ZjAdmServicePage servicePage = new ZjAdmServicePage();
        BeanUtils.copyProperties(servicePageVo, servicePage);
        if (null != servicePage.getId()) {
            this.updateById(servicePage);
            return;
        }
        servicePage.setId(IdWorker.getId());
        this.save(servicePage);
    }

    // 获取服务页面列表
    @Override
    public Map<String, Object> getList(ServicePageSelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        QueryWrapper<ZjAdmServicePage> wrapper = new QueryWrapper<>();
        wrapper.select("id", "type", "name", "page_title", "path", "is_disabled AS disabled", "gmt_create", "gmt_update");
        if (null != selectVo.getType()) {
            wrapper.eq("type", selectVo.getType());
        }
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        if (StringUtils.isNotBlank(selectVo.getPageTitle())) {
            wrapper.like("page_title", selectVo.getPageTitle());
        }
        Page<ZjAdmServicePage> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<ServicePageListVo> voList = page.getRecords().stream().map(t -> {
            ServicePageListVo vo = new ServicePageListVo();
            BeanUtils.copyProperties(t, vo);
            vo.setStatus(t.getDisabled());
            vo.setCreateTime(t.getGmtCreate());
            vo.setUpdateTime(t.getGmtUpdate());
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据服务页面ID获取服务页面详情
    @Override
    public ServicePageVo getDetail(Long id) {
        ZjAdmServicePage page = this.getById(id);
        Assert.isTrue(null != page, "服务页面ID参数["+ id +"]的页面数据不存在");
        ServicePageVo vo = new ServicePageVo();
        BeanUtils.copyProperties(page, vo);
        vo.setStatus(page.getDisabled());
        return vo;
    }
}
